'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE, BOOLEAN } = app.Sequelize;

  const User = app.model.define('user', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    account: STRING(30),
    role_id: INTEGER,
    password: STRING(50),
    name: STRING(30),
    email: STRING(50),
    photo: STRING(200), // 头像
    phone: STRING(30),
    description: STRING(255),
    status: BOOLEAN,
    created_at: DATE,
    updated_at: DATE,
  });

  User.associate = function() {
    app.model.User.belongsTo(app.model.Role, { foreignKey: 'roleId' });
    app.model.User.belongsTo(app.model.Roleresource, { foreignKey: 'roleId', targetKey: 'roleId' });
  };

  User.validate = {
    rules: {
      account: {
        required: true,
        minlength: 2, maxlength: 30,
      },
      role_id: 'required',
      // password: {  // 为了兼容编辑账号时，允许密码为空，所以取消密码字段校验
      //   required: true,
      //   minlength: 6, maxlength: 30,
      // },
      name: {
        required: true,
        minlength: 2, maxlength: 30,
      },
      email: {
        required: true,
        email: true,
      },
      status: 'required',
    },
  };

  return User;
};
